import { getExamType, getExamSubject, getQuestionType ,submitQuestion,insertQuestions,examQuestions,conditionQuestions, updataExam} from '@/service';
import { Effect, Reducer} from 'umi';
import { IQuestionType, IExamType, ISubject ,IExamQuestions} from '@/utils/interface'

export interface QuestionModelState {
  questionTypes: IQuestionType[],
  subjects: ISubject[],
  examTypes: IExamType [],
  examQuestions:IExamQuestions[],


}
export interface QuestionModelType {
  namespace: 'question';
  state: QuestionModelState;
  effects: {
    getQuestionType: Effect;
    getExamSubject: Effect;
    getExamType: Effect;
    submitQuestion:Effect;
    insertQuestionsType:Effect;
    getExamQuestions:Effect;
    getCondition:Effect;
    setUpdataExam:Effect;
  };
  reducers: {
    save: Reducer<QuestionModelState>;
  };
}
const QuestionModel: QuestionModelType = {
  namespace: 'question',
  state: {
    questionTypes: [],
    subjects: [],
    examTypes: [],
    examQuestions:[],
  },
  // 异步操作，理解为vuex中的action, async/await理解为generator函数的语法糖
  effects: {
<<<<<<< HEAD:项目代码/Backstage Management System/src/models/question.ts
    // 获取试题列表
=======
>>>>>>> 925273379b18744fc15f250871f012fa5f2db418:项目代码/liuexam_cms/src/models/question.ts
    *getQuestionType({  }, { call, put }) {
      let result = yield call(getQuestionType);
      if (result.data) {
        yield put({
          type: 'save',
          payload: { questionTypes: result.data }
        })
      }
    },
<<<<<<< HEAD:项目代码/Backstage Management System/src/models/question.ts
    // 获取所有课程
=======
>>>>>>> 925273379b18744fc15f250871f012fa5f2db418:项目代码/liuexam_cms/src/models/question.ts
    *getExamSubject({  }, { call, put }) {
      let result = yield call(getExamSubject);
      if (result.data) {
        yield put({
          type: 'save',
          payload: { subjects: result.data }
        })
      }
    },
<<<<<<< HEAD:项目代码/Backstage Management System/src/models/question.ts
    // 获取所有考试类型
=======
>>>>>>> 925273379b18744fc15f250871f012fa5f2db418:项目代码/liuexam_cms/src/models/question.ts
    *getExamType({  }, { call, put }) {
      let result = yield call(getExamType);
      if (result.data) {
        yield put({
          type: 'save',
          payload: { examTypes: result.data }
        })
      }
    },
<<<<<<< HEAD:项目代码/Backstage Management System/src/models/question.ts
    // 添加试题
=======
>>>>>>> 925273379b18744fc15f250871f012fa5f2db418:项目代码/liuexam_cms/src/models/question.ts
    *submitQuestion({payload},{call,put}){
      let result = yield call(submitQuestion, payload);
      return result;
    },
    //添加试题类型
    *insertQuestionsType({payload},{call,put}){
      let result = yield call(insertQuestions, payload);
      return result
    },
<<<<<<< HEAD:项目代码/Backstage Management System/src/models/question.ts
    
    //获取所有试题
=======
>>>>>>> 925273379b18744fc15f250871f012fa5f2db418:项目代码/liuexam_cms/src/models/question.ts
    *getExamQuestions({  }, { call, put }) {
      let result = yield call(examQuestions);
      if (result.data) {
        yield put({
          type: 'save',
          payload: { examQuestions: result.data }
        })
      }
    },
<<<<<<< HEAD:项目代码/Backstage Management System/src/models/question.ts
    //按条件获取试题
=======
>>>>>>> 925273379b18744fc15f250871f012fa5f2db418:项目代码/liuexam_cms/src/models/question.ts
    *getCondition({ payload }, { call, put }){
      let result = yield call(conditionQuestions, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: { examQuestions: result.data }
        })
      }
    },
<<<<<<< HEAD:项目代码/Backstage Management System/src/models/question.ts
    //更新试题
=======
>>>>>>> 925273379b18744fc15f250871f012fa5f2db418:项目代码/liuexam_cms/src/models/question.ts
    *setUpdataExam({ payload }, { call, put }){
      let result = yield call( updataExam, payload);
      return result
    },

    



  },
  // 同步操作，理解为vuex中的mutation
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    }
  }
};
export default QuestionModel;
